

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="caoyusang">
  <meta name="keywords" content="">
  <title>leetcode题解之链表 - caoyusang的科研日常</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>caoyusang</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/7.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-01-21 20:44" pubdate>
        2021年1月21日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      23
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">leetcode题解之链表</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2 个月前
                
              </p>
            
            <div class="markdown-body" id="post-body">
              <h5 id="1290-将链表中的二进制值转成整数"><a href="#1290-将链表中的二进制值转成整数" class="headerlink" title="1290.将链表中的二进制值转成整数"></a>1290.将链表中的二进制值转成整数</h5><h6 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getDecimalValue</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        <span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">int</span> cnt = -<span class="hljs-number">1</span>;
        ListNode p = head;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span>)&#123;
            cnt += <span class="hljs-number">1</span>;
            p = p.next;
        &#125;
        <span class="hljs-keyword">while</span> (head != <span class="hljs-keyword">null</span>)&#123;
            sum += head.val * Math.pow(<span class="hljs-number">2</span>, cnt--);
            head = head.next;
        &#125;
        <span class="hljs-keyword">return</span> sum;
    &#125;
&#125;</code></pre>
<h5 id="206-翻转链表"><a href="#206-翻转链表" class="headerlink" title="206.翻转链表"></a>206.翻转链表</h5><h6 id="题解-1"><a href="#题解-1" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">reverseList</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        <span class="hljs-comment">// head insert</span>
        ListNode res = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        res.next = <span class="hljs-keyword">null</span>;
        ListNode p = res.next;
        <span class="hljs-keyword">while</span>(head != <span class="hljs-keyword">null</span>)&#123;
            res.next = <span class="hljs-keyword">new</span> ListNode(head.val);
            res.next.next = p;
            p = res.next;
            head = head.next;
        &#125;
        <span class="hljs-keyword">return</span> res.next;
    &#125;
&#125;</code></pre>
<h5 id="92-翻转链表Ⅱ"><a href="#92-翻转链表Ⅱ" class="headerlink" title="92.翻转链表Ⅱ"></a>92.翻转链表Ⅱ</h5><h6 id="题解-2"><a href="#题解-2" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs plain"></code></pre>
<h5 id="203-移除链表元素"><a href="#203-移除链表元素" class="headerlink" title="203.移除链表元素"></a>203.移除链表元素</h5><h6 id="题解-3"><a href="#题解-3" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">removeElements</span><span class="hljs-params">(ListNode head, <span class="hljs-keyword">int</span> val)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span>) &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        &#125;
        ListNode pre = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        pre.next = head;
        ListNode p = head, q = pre;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (p.val == val)&#123;
                q.next = p.next;
                p = p.next;
                <span class="hljs-keyword">continue</span>;
            &#125;
            q = q.next;
            p = p.next;
        &#125;
        <span class="hljs-keyword">return</span> pre.next;
    &#125;
&#125;</code></pre>
<h5 id="234-判断回文链表"><a href="#234-判断回文链表" class="headerlink" title="234.判断回文链表"></a>234.判断回文链表</h5><h6 id="题解-4"><a href="#题解-4" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isPalindrome</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        <span class="hljs-comment">// base situation</span>
        <span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span> || head.next == <span class="hljs-keyword">null</span>) &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        &#125;
        ListNode revHead = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        revHead.next = <span class="hljs-keyword">null</span>;
        ListNode p = revHead.next, q = head; 
        <span class="hljs-comment">// head insert(reverse linked list)</span>
        <span class="hljs-keyword">while</span> (q != <span class="hljs-keyword">null</span>)&#123;
            revHead.next = <span class="hljs-keyword">new</span> ListNode(q.val);
            revHead.next.next = p;
            p = revHead.next;
            q = q.next;
        &#125;
        <span class="hljs-comment">// compare one by one</span>
        <span class="hljs-keyword">while</span> (revHead.next != <span class="hljs-keyword">null</span> &amp;&amp; head != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (revHead.next.val != head.val)&#123;
                <span class="hljs-keyword">break</span>;
            &#125;
            revHead = revHead.next;
            head = head.next;
        &#125;
        <span class="hljs-comment">// attention</span>
        <span class="hljs-keyword">return</span> head == <span class="hljs-keyword">null</span>;
    &#125;
&#125;</code></pre>
<h5 id="328-奇偶链表"><a href="#328-奇偶链表" class="headerlink" title="328.奇偶链表"></a>328.奇偶链表</h5><h6 id="题解-5"><a href="#题解-5" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">oddEvenList</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span> || head.next == <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">return</span> head;
        &#125;
        ListNode p = head, q = head.next, pre = q;
        <span class="hljs-keyword">while</span> (q != <span class="hljs-keyword">null</span> &amp;&amp; q.next != <span class="hljs-keyword">null</span>)&#123;
            p.next = q.next;
            p = p.next;
            q.next = p.next;
            q = q.next;
        &#125;
        p.next = pre;
        <span class="hljs-keyword">return</span> head;

    &#125;
&#125;</code></pre>
<h5 id="141-循环链表"><a href="#141-循环链表" class="headerlink" title="141.循环链表"></a>141.循环链表</h5><h6 id="题解-6"><a href="#题解-6" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">hasCycle</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span> || head.next == <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
        &#125;
        ListNode fastPtr = head, slowPtr = head;
        <span class="hljs-keyword">while</span>(fastPtr != <span class="hljs-keyword">null</span> &amp;&amp; fastPtr.next != <span class="hljs-keyword">null</span>)&#123;
            fastPtr = fastPtr.next.next;
            slowPtr = slowPtr.next;
            <span class="hljs-keyword">if</span> (fastPtr == slowPtr)&#123;
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            &#125;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
        
    &#125;
&#125;</code></pre>
<h5 id="142-循环链表Ⅱ"><a href="#142-循环链表Ⅱ" class="headerlink" title="142.循环链表Ⅱ"></a>142.循环链表Ⅱ</h5><h6 id="题解-7"><a href="#题解-7" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">detectCycle</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        HashSet&lt;ListNode&gt; hashset = <span class="hljs-keyword">new</span> HashSet&lt;&gt;();
        ListNode pre = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        pre.next = head;
        ListNode p = pre.next;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (hashset.contains(p))&#123;
                <span class="hljs-keyword">return</span> p;
            &#125;
            hashset.add(p);
            p = p.next;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        
    &#125;
&#125;</code></pre>
<h5 id="876-链表的中间节点"><a href="#876-链表的中间节点" class="headerlink" title="876.链表的中间节点"></a>876.链表的中间节点</h5><h6 id="题解-8"><a href="#题解-8" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">middleNode</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        HashMap&lt;Integer, ListNode&gt; map = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
        <span class="hljs-keyword">int</span> cnt = -<span class="hljs-number">1</span>;
        <span class="hljs-keyword">while</span> (head != <span class="hljs-keyword">null</span>)&#123;
            cnt += <span class="hljs-number">1</span>;
            map.put(cnt, head);
            head = head.next;
        &#125;
        <span class="hljs-keyword">return</span> map.get((cnt + <span class="hljs-number">1</span>) / <span class="hljs-number">2</span>);
    &#125;
&#125;</code></pre>
<h5 id="21-有序链表的合并"><a href="#21-有序链表的合并" class="headerlink" title="21.有序链表的合并"></a>21.有序链表的合并</h5><h6 id="题解-9"><a href="#题解-9" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeTwoLists</span><span class="hljs-params">(ListNode l1, ListNode l2)</span> </span>&#123;
        ListNode pre = <span class="hljs-keyword">new</span> ListNode(-<span class="hljs-number">1</span>);
        ListNode p = pre;
        <span class="hljs-keyword">while</span> (l1 != <span class="hljs-keyword">null</span> &amp;&amp; l2 != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span>(l1.val &lt;= l2.val)&#123;
                p.next = <span class="hljs-keyword">new</span> ListNode(l1.val);
                l1 = l1.next;
            &#125; <span class="hljs-keyword">else</span> &#123;
                p.next = <span class="hljs-keyword">new</span> ListNode(l2.val);
                l2 = l2.next;
            &#125;
            p = p.next;
        &#125;
        p.next = (l1 != <span class="hljs-keyword">null</span>) ? l1 : l2;
        <span class="hljs-keyword">return</span> pre.next;
    &#125;
&#125;</code></pre>
<h5 id="83-移除有序链表中的重复元素"><a href="#83-移除有序链表中的重复元素" class="headerlink" title="83.移除有序链表中的重复元素"></a>83.移除有序链表中的重复元素</h5><h6 id="题解-10"><a href="#题解-10" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">deleteDuplicates</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span>) <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        ListNode pre = <span class="hljs-keyword">new</span> ListNode(-<span class="hljs-number">1</span>);
        pre.next = head;
        ListNode p = head.next;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (p.val == head.val)&#123;
                p = p.next;
                head.next = p;
                <span class="hljs-keyword">continue</span>;
            &#125;
            head = head.next;
            p = p.next;
        &#125;
        <span class="hljs-keyword">return</span> pre.next;
    &#125;
&#125;</code></pre>
<h5 id="160-两个链表的交点"><a href="#160-两个链表的交点" class="headerlink" title="160.两个链表的交点"></a>160.两个链表的交点</h5><h6 id="题解-11"><a href="#题解-11" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode(int x) &#123;</span>
<span class="hljs-comment"> *         val = x;</span>
<span class="hljs-comment"> *         next = null;</span>
<span class="hljs-comment"> *     &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-comment">// Set&lt;ListNode&gt; set = new HashSet&lt;&gt;();</span>
    <span class="hljs-comment">// public ListNode getIntersectionNode(ListNode headA, ListNode headB) &#123;</span>
    <span class="hljs-comment">//     while (headB != null)&#123;</span>
    <span class="hljs-comment">//         set.add(headB);</span>
    <span class="hljs-comment">//         headB = headB.next;</span>
    <span class="hljs-comment">//     &#125;</span>
    <span class="hljs-comment">//     while (headA != null)&#123;</span>
    <span class="hljs-comment">//         if (set.contains(headA))&#123;</span>
    <span class="hljs-comment">//             return headA;</span>
    <span class="hljs-comment">//         &#125;</span>
    <span class="hljs-comment">//         headA = headA.next;</span>
    <span class="hljs-comment">//     &#125;</span>
    <span class="hljs-comment">//     return null;</span>
    <span class="hljs-comment">// &#125;</span>
    
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">getIntersectionNode</span><span class="hljs-params">(ListNode headA, ListNode headB)</span> </span>&#123;
        ListNode p = headA;
        ListNode q = headB;
        <span class="hljs-keyword">while</span> (p != q)&#123;
            p = p == <span class="hljs-keyword">null</span>? headB : p.next;
            q = q == <span class="hljs-keyword">null</span>? headA : q.next;
        &#125;
        <span class="hljs-keyword">return</span> p;
    &#125;
&#125;</code></pre>
<h5 id="1669-链表的部分替换"><a href="#1669-链表的部分替换" class="headerlink" title="1669.链表的部分替换"></a>1669.链表的部分替换</h5><h6 id="题解-12"><a href="#题解-12" class="headerlink" title="题解"></a>题解</h6><pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeInBetween</span><span class="hljs-params">(ListNode list1, <span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b, ListNode list2)</span> </span>&#123;
        ListNode res = <span class="hljs-keyword">new</span> ListNode(-<span class="hljs-number">1</span>);
        res.next = list1;
        ListNode pre = res;
        ListNode p = list1;
        ListNode q = list2;
        <span class="hljs-keyword">int</span> cnt = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span> &amp;&amp; cnt &lt; b)&#123;
            <span class="hljs-keyword">if</span> (cnt &lt; a)&#123;
                pre = pre.next;
            &#125;
            p = p.next;
            cnt++;
        &#125;
        
        pre.next = list2;
        <span class="hljs-keyword">while</span> (q.next != <span class="hljs-keyword">null</span>) &#123;
            q = q.next;
        &#125;
        q.next = p.next;
        
        <span class="hljs-keyword">return</span> res.next;
    &#125;
&#125;</code></pre>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E7%AE%97%E6%B3%95/">算法</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/leetcode/">leetcode</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/01/25/leetcode%E9%A2%98%E8%A7%A3%E4%B9%8B%E6%95%B0%E7%BB%84/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">leetcode题解之数组</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/01/18/leetcode%E9%A2%98%E8%A7%A3%E4%B9%8B%E4%BA%8C%E5%8F%89%E6%A0%91/">
                        <span class="hidden-mobile">leetcode题解之二叉树</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "leetcode题解之链表&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.staticfile.org/mathjax/3.0.5/es5/tex-svg.js" ></script>

  











</body>
</html>
